//
//  NewsViewController.m
//  HWMovie
//
//  Created by Yiqiao on 15/7/21.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "NewsViewController.h"
#import "newsDetailViewController.h"
#import "NewsCell.h"
#import "NewsModal.h"
#import "common.h"
#import "UIViewExt.h"
#import "detailViewController.h"
#define w [UIScreen mainScreen].bounds.size.width
#define h [UIScreen mainScreen].bounds.size.height
#define navH 64;
#define barH 49;
static NSString *newsCellId = @"newsCellId";

@interface NewsViewController ()

@end

@implementation NewsViewController

#pragma mark -  ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新闻";
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createTable];
    [self _createHead];
    [self _loadData];

    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_createHead{
    
    NewsModal *headerModal = _newsModalArray[0];
    
    _headLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, w, 40)];
    
    _headLabel.text = [NSString stringWithFormat:@"%@",headerModal.title];
    [_headLabel setTextAlignment:NSTextAlignmentCenter];
    _headLabel.font = [UIFont systemFontOfSize:18];
    _headLabel.textColor = [UIColor whiteColor];
    _headLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;//自动布局，自适应顶部
    
    _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200, w, 200)];
    
    _headImageView .contentMode = UIViewContentModeScaleAspectFill;
    
    NSString *headerImageURL = headerModal.image;
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:headerImageURL]];
    
    [_tableView addSubview:_headImageView];
    
    [_headImageView addSubview:_headLabel];
    
}
#pragma mark - createSubViews
- (void)_createTable{
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"NewsCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:newsCellId];
    _tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
}


#pragma mark - loadData
- (void)_loadData{
    
    _newsModalArray = [[NSMutableArray alloc]init];
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"news_list.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSArray *newsArray =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    for (NSDictionary *dic in newsArray) {
        NewsModal *modal = [[NewsModal alloc]init];
        [modal setValuesForKeysWithDictionary:dic];
        [_newsModalArray addObject:modal];
    }
    
    
    NewsModal *modal = _newsModalArray[0];
    _headLabel.text = modal.title;
    _headImageView.image = [UIImage imageNamed:modal.image];
    NSURL *url = [NSURL URLWithString:modal.image];
    [_headImageView sd_setImageWithURL:url];
}
#pragma mark - TableView Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _newsModalArray.count-1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  90.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:newsCellId forIndexPath:indexPath];
    cell.news = _newsModalArray[indexPath.row+1];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return  cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat y = scrollView.contentOffset.y;//根据实际选择加不加上NavigationBarHight（44、64 或者没有导航条）
    if (y < -200) {
        CGRect frame = _headImageView.frame;
        frame.origin.y = y;
        frame.size.height =  -y;//contentMode = UIViewContentModeScaleAspectFill时，高度改变宽度也跟着改变
        _headImageView.frame = frame;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewsModal *model = [[NewsModal alloc]init];
    model = _newsModalArray[indexPath.row];
    if (model.type==1) {
        detailViewController *new1 = [[detailViewController alloc]init];
        [self.navigationController pushViewController:new1 animated:YES];
        
    }
    else{
        newsDetailViewController *new2 = [[newsDetailViewController alloc]init];
        [self.navigationController pushViewController:new2 animated:YES];
    }
    
}




@end
